package com.c201801020208.ybl.User;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.c201801020208.ybl.Company.Activity.Main2Activity;
import com.c201801020208.ybl.R;
import com.c201801020208.ybl.Company.Activity.JobInfoActivity;
import com.c201801020208.ybl.Tables.CompanyInfo;
import com.c201801020208.ybl.Tables.MyJobInfo;
import com.c201801020208.ybl.User.Activity.MainActivity;
import com.c201801020208.ybl.User.Activity.WebActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class jianliFragment extends Fragment {
    private Banner banner;
    private ArrayList<Integer> images;
    RecyclerView mRecyclerView;
    List<CompanyInfo> mList = new ArrayList<>();
    MyAdapter mMyAdapter = new MyAdapter();
    MainActivity mainActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jianli, container, false);

        return view;
    }

    private void initBanneer() {
        //设置图片资源:url或本地资源
        images = new ArrayList<>();
        images.add(R.drawable.activity1);
        images.add(R.drawable.activity2);
        images.add(R.drawable.activity3);
        images.add(R.drawable.activity4);
        //设置图片加载器
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context.getApplicationContext())
                        .load(path)
                        .into(imageView);
            }
        });
        //设置样式,默认为:Banner.NOT_INDICATOR(不显示指示器和标题)
        //可选样式如下:
        //1. Banner.CIRCLE_INDICATOR    显示圆形指示器
        //2. Banner.NUM_INDICATOR   显示数字指示器
        //3. Banner.NUM_INDICATOR_TITLE 显示数字指示器和标题
        //4. Banner.CIRCLE_INDICATOR_TITLE  显示圆形指示器和标题
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置轮播图片间隔时间（不设置默认为2000）
        banner.setDelayTime(3000);
        //设置是否自动轮播（不设置则默认自动)
        banner.isAutoPlay(true);
        //设置是否允许手动滑动轮播图
        banner.setViewPagerIsScroll(true);
        banner.setImages(images);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                startActivity(intent);
            }
        }).start();
    }

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        banner = getActivity().findViewById(R.id.banner);
        initBanneer();


        mRecyclerView = getActivity().findViewById(R.id.recycle1);
        mRecyclerView.setAdapter(mMyAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void onResume() {
        super.onResume();
        mList.removeAll(mList);
        String Myname = mainActivity.getUsername();
        BmobQuery<MyJobInfo> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("Myname", Myname);
        categoryBmobQuery.findObjects(new FindListener<MyJobInfo>() {
            @Override
            public void done(List<MyJobInfo> object, BmobException e) {
                if (e == null) {
                    Toast.makeText(getContext(), "查询成功：", Toast.LENGTH_LONG).show();
                    for (int i = 0; i < object.size(); i++) {
                        CompanyInfo companyInfo = new CompanyInfo();
                        companyInfo.setMyname(object.get(i).getMyname());
                        companyInfo.setUsername(object.get(i).getUsername());
                        companyInfo.setUsername(object.get(i).getUsername());
                        companyInfo.setAddress(object.get(i).getAddress());
                        companyInfo.setWorktime(object.get(i).getWorktime());
                        companyInfo.setWorkexperience(object.get(i).getWorkexperience());
                        companyInfo.setCompanyname(object.get(i).getCompanyname());
                        companyInfo.setMoney(object.get(i).getMoney());
                        companyInfo.setType(object.get(i).getType());
                        companyInfo.setJobname(object.get(i).getJobname());
                        companyInfo.setEducation(object.get(i).getEducation());
                        companyInfo.setDescribe(object.get(i).getDescribe());
                        mList.add(companyInfo);
                    }
                    mMyAdapter.notifyDataSetChanged();
                } else {
                    Log.e("BMOB", e.toString());
                    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.chatlayout, parent, false);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull final MyViewHoder holder, int position) {
            CompanyInfo companyInfo = mList.get(position);
            holder.address.setText(companyInfo.getAddress());
            holder.companyname.setText(companyInfo.getCompanyname());
            holder.jobname.setText(companyInfo.getJobname());
            holder.education.setText(companyInfo.getEducation());
            holder.workexperience.setText(companyInfo.getWorkexperience());
            holder.content.setText(companyInfo.getDescribe());
            holder.salary.setText(companyInfo.getMoney());
            holder.worktime.setText(companyInfo.getWorktime());
            holder.jobtype.setText(companyInfo.getType());
            holder.username.setText(companyInfo.getUsername());
            holder.myrootview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {       //item控件监听，intent传值给edit
                    Intent intent = new Intent(getActivity(), JobInfoActivity.class);
                    intent.putExtra("companyInfo", companyInfo);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView jobname;
        TextView companyname;
        TextView salary;
        TextView username;
        TextView address;
        TextView worktime;
        TextView jobtype;
        TextView education;
        TextView workexperience;
        TextView content;
        CardView myrootview;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            companyname = itemView.findViewById(R.id.textView13);
            salary = itemView.findViewById(R.id.textView14);
            username = itemView.findViewById(R.id.textView7);
            address = itemView.findViewById(R.id.textView8);
            worktime = itemView.findViewById(R.id.textView15);
            jobtype = itemView.findViewById(R.id.textView16);
            content = itemView.findViewById(R.id.textView17);
            workexperience = itemView.findViewById(R.id.textView18);
            education = itemView.findViewById(R.id.textView20);
            myrootview = itemView.findViewById(R.id.rootview);
            jobname = itemView.findViewById(R.id.textView9);
        }
    }

}
